#include <iostream.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>

#define IDC_EDIT_MIN		108
#define IDC_EDIT_TOTAL		109
#define IDC_EDIT_ROUTE		110

using namespace std;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

char szClassName[ ] = "WindowsApp";
HWND hEditMin=NULL;
HWND hEditTotal=NULL;
HWND hEditRoute=NULL;
void TakeScreenShot(char* filename)
{
	keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
	keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
	HBITMAP h;
	
	OpenClipboard(NULL);
	h = (HBITMAP)GetClipboardData(CF_BITMAP);
	CloseClipboard();
	HDC hdc = NULL;
    FILE*fp=NULL;
    LPVOID pBuf=NULL;
    BITMAPINFO bmpInfo;
    BITMAPFILEHEADER bmpFileHeader;
    do
	{ 
		hdc=GetDC(NULL);
        ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
        bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
        GetDIBits(hdc,h,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); 
        if(bmpInfo.bmiHeader.biSizeImage<=0)
        {
			bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
        }
        if((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
        {
            MessageBox( NULL, "Unable to Allocate Bitmap Memory", "Error", MB_OK|MB_ICONERROR);
            break;
        } 
        bmpInfo.bmiHeader.biCompression=BI_RGB;
        GetDIBits(hdc,h,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
        if((fp = fopen(filename,"wb"))==NULL)
        {
	        MessageBox( NULL, "Unable to Create Bitmap File", "Error", MB_OK|MB_ICONERROR);
            break;
        } 
        bmpFileHeader.bfReserved1=0;
        bmpFileHeader.bfReserved2=0;
        bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
        bmpFileHeader.bfType='MB';
        bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
        fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
        fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
        fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp); 
    }while(false); 
		
    if(hdc)ReleaseDC(NULL,hdc); 
    if(pBuf) free(pBuf); 
    if(fp)fclose(fp);
}
void TakeScreenShots(int numberPics, int MiliSeconds, char route[])
{
    char root[256];
    //sprintf (route, "%s", root);
    char sub[5] = ".bmp";
    for(int i = 0; i < numberPics; i++)
    {
         sprintf (route, "%s%d", route, i);
         MessageBox( NULL, route, "Error", MB_OK|MB_ICONERROR);
         strncat (route, sub, 32);
         TakeScreenShot(route);
         Sleep(MiliSeconds);
    }
}

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
    HWND hwnd; 
    MSG messages;
    WNDCLASSEX wincl;

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx(
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Auto Screenshot Capture",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544, 375,                 /* and height in pixels */
           NULL,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    ShowWindow (hwnd, nFunsterStil);
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{       
    static HWND hwndButton = 0;
    HDC hdc;
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
             hdc = GetDC (hwnd);
             SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
             ReleaseDC (hwnd, hdc);
             hwndButton = CreateWindow("button", "Take Screenshots", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
             10, 10, 200, 100, hwnd, (HMENU) 1, ((LPCREATESTRUCT) lParam)->hInstance, NULL );
             
             hEditMin = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_TABSTOP|WS_CHILD|WS_VISIBLE|ES_NUMBER,
				220, 10, 100, 25, hwnd, (HMENU)IDC_EDIT_MIN, GetModuleHandle(NULL), NULL);
			 SendMessage(hEditMin, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), (LPARAM)TRUE);
			 SendMessage(hEditMin, WM_SETTEXT, NULL, (LPARAM)"1");
			 
			 hEditTotal = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_TABSTOP|WS_CHILD|WS_VISIBLE,
				220, 40, 100, 25, hwnd, (HMENU)IDC_EDIT_TOTAL, GetModuleHandle(NULL), NULL);
			 SendMessage(hEditTotal, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), (LPARAM)TRUE);
			 SendMessage(hEditTotal, WM_SETTEXT, NULL, (LPARAM)"0");
			 
			 hEditRoute = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_TABSTOP|WS_CHILD|WS_VISIBLE,
				220, 70, 100, 25, hwnd, (HMENU)IDC_EDIT_ROUTE, GetModuleHandle(NULL), NULL);
			 SendMessage(hEditRoute, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), (LPARAM)TRUE);
			 SendMessage(hEditRoute, WM_SETTEXT, NULL, (LPARAM)"");
        break;
        case WM_COMMAND:
             if (LOWORD(wParam) == 1 && HIWORD(wParam) == BN_CLICKED && (HWND) lParam == hwndButton)
             {
                char MinT[6];
                char TotalT[6];
                char route[256];
				ZeroMemory(route,sizeof(route));	
                int Min;
                int Total;
                SendMessage(hEditMin, WM_GETTEXT, 6, reinterpret_cast<int>(MinT));
                SendMessage(hEditTotal, WM_GETTEXT, 6, reinterpret_cast<int>(TotalT));
                SendMessage(hEditRoute, WM_GETTEXT, 256, reinterpret_cast<LPARAM>(route));
                Min = atoi(MinT);
                Total= atoi(TotalT);
                TakeScreenShots(Total, Min*1000, route);
             }
             return 0;
        break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
